#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
    int pid = -1;
    char *mode = NULL;
    if (argc != 2)
    {
        printf("Usage: %s <mode>\n", argv[0]);
        return 1;
    }
    mode = argv[1];
    if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a')
    {
        perror("Invalid mode\n");
        return 1;
    }
    pid = fork();
    if (pid == -1)
    {
        perror("fork error\n");
        return 1;
    }
    FILE *file = fopen("test.txt", argv[1]);
    if (file == NULL)
    {
        fprintf(stderr, "pid %d:fopen error\n", getpid());
        return 1;
    }
    if (mode[0] == 'w')
    {
        fprintf(file, "pid %d:writing\n", getpid());
    }
    else if (mode[0] == 'r')
    {
        char buffer[100];
        fscanf(file, "%s", buffer);
        printf("pid %d:reading: %s\n", getpid(), buffer);
    }
    else if (mode[0] == 'a')
    {
        fprintf(file, "pid %d:appending\n", getpid());
    }
    return 0;
}